package com.br.aiops.storage.counter;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface CounterRepository extends JpaRepository<CounterDTO,Integer> {

    /**
     * 根据partition获取近一小时未恢复数据
     * @return
     */
    @Query(nativeQuery = true, value = "select * from log_alert_counter where is_resume = 0 and `partition` = ?1")
    List<CounterDTO> findByPartition(int partition);

    /**
     * 获取已恢复数据
     * @return
     */
    @Query(nativeQuery = true, value = "select * from log_alert_counter where is_resume = 1")
    List<CounterDTO> findByResume();

    CounterDTO findByKey(String key);

    @Query(nativeQuery = true, value = "update log_alert_counter set is_resume = 0 where counter_key = ?1")
    @Modifying
    @Transactional
    void setNotResume(String key);

    @Query(nativeQuery = true, value = "update log_alert_counter set is_resume = 1 where counter_key = ?1")
    @Modifying
    @Transactional
    void resume(String key);
}
